<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/beans/Kid.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/beans/Family.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/beans/Communication.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/dao/UkkepukDao.php'); 

	$kid = new Kid();
	$ukkepukDao = new UkkepukDao();
	
	$kid->name = $_POST['name'];
	$kid->firstname = $_POST['firstName'];
	
	$kid->street = $_POST['street'];
	$kid->number = $_POST['number'];
	$kid->postal = $_POST['postal'];
	$kid->city = $_POST['city'];
	
	$comTypePattern = "/^comType([0-9])+$/";
	$comPattern = "/^com([0-9])+$/";
	
	$famTypePattern = "/^famType([0-9])+$/";
	$famNamePattern = "/^famName([0-9])+$/";
	$famFirstNamePattern = "/^famFirstName([0-9])+$/";
	
	$famComTypePattern = "/^fam([0-9]+)comType([0-9])+$/";
	$famComPattern = "/^fam([0-9]+)com([0-9])+$/";

	$comArray = Array();
	$comTypeArray = Array();
	
	foreach($_POST as $key => $value){
		//parse comType
		preg_match($comTypePattern, $key, $matches);
		if(sizeof($matches) == 2){
			$index = $matches[1];
			$kid->addCommunicationTypeOnPos($value, $index);
		}
		
		//parse com
		preg_match($comPattern, $key, $matches);
		if(sizeof($matches) == 2){
			$index = $matches[1];
			$kid->addCommunicationValueOnPos($value, $index);
		}
		
		//parse famType
		preg_match($famTypePattern, $key, $matches);
		if(sizeof($matches) == 2){
			$index = $matches[1];
			$kid->addFamilyTypeOnPos($value, $index);
		}
		
		//parse famName
		preg_match($famNamePattern, $key, $matches);
		if(sizeof($matches) == 2){
			$index = $matches[1];
			$kid->addFamilyNameOnPos($value, $index);
		}
		
		//parse famFirstName
		preg_match($famFirstNamePattern, $key, $matches);
		if(sizeof($matches) == 2){
			$index = $matches[1];
			$kid->addFamilyFirstNameOnPos($value, $index);
		}
		
		//parse fam.comType
		preg_match($famComTypePattern, $key, $matches);
		if(sizeof($matches) == 3){
			$famIndex = $matches[1];
			$comIndex = $matches[2];
			$kid->addCommunicationTypeForFamilyOnPos($value, $famIndex, $comIndex);
		}
		
		//parse fam.com
		preg_match($famComPattern, $key, $matches);
		if(sizeof($matches) == 3){
		
			$famIndex = $matches[1];
			$comIndex = $matches[2];
			$kid->addCommunicationValueForFamilyOnPos($value, $famIndex, $comIndex);
		}
	}
	
	//save foto if any
	if ($_FILES["photo"]["error"] == 0) {
		move_uploaded_file($_FILES["photo"]["tmp_name"],
							$_SERVER['DOCUMENT_ROOT'].'/ukkepuk/webcontent/images/kids/' . $_FILES["photo"]["name"]);
		echo "Stored in: " . "upload/" . $_FILES["photo"]["name"];
	}
	
	echo "debug";
	$kid->toString();
	
	$ukkepukDao->saveNewKid($kid);

?>